package AgentTravel.Behaviours;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import AgentTravel.LogicObjects.User;
import AgentTravel.LogicObjects.UserInfoForServer;
import AgentTravel.Messages.PlanMyTripMessage;
import AgentTravel.Messages.RegisterMessage;
import AgentTravel.Messages.SaveUserMessage;
import AgentTravel.Messages.ServerConfirmationInfo;
import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;
import jade.wrapper.AgentController;
import jade.wrapper.StaleProxyException;

public class ServerGetModyficationsBehaviour extends CyclicBehaviour
{
	private Map<String,UserInfoForServer> users;
	private Map<String,String> passLogin;
	private Map<String,AID> userGui;
	
	public ServerGetModyficationsBehaviour(Map<String, UserInfoForServer> users2,Map<String, String> passLogin2,Map <String,AID> userGui)
	{
		this.users=users2;
		this.passLogin=passLogin2;
		this.userGui=userGui;
	}
	
	
	@Override
	public void action() 
	{
		MessageTemplate mt=MessageTemplate.MatchPerformative(ACLMessage.INFORM);
		
		ACLMessage msg = myAgent.receive(mt);
		
		if (msg != null) 
		{
			SaveUserMessage msgObj=null;
		
			try
			{
				msgObj=(SaveUserMessage)msg.getContentObject();
			}
			catch(UnreadableException ex)
			{
				System.out.println("UUUUnreadable :(");
			}
			
			
			if(msgObj!=null)
			{
				System.out.println("SERWER: Dostalem prosbe o zapisanie zmian");
				if(!users.containsKey(msgObj.UserName) || !passLogin.get(msgObj.UserName).equals(msgObj.Password))
				{
					//odrzucenie zapisu
					ACLMessage reply=new ACLMessage(ACLMessage.REFUSE);
					reply.addReceiver(msg.getSender());
					myAgent.send(reply);
				}
				else
				{
					AgentController agentH=users.get(msgObj.UserName).yourHostAgent;
					AgentController agentP=users.get(msgObj.UserName).yourVisitorAgent;
					
					users.put(msgObj.UserName,new UserInfoForServer(agentH, agentP, msgObj.user));
					//usuwanie poprzednich wpisow z userGui
					LinkedList<String> toRemove=new LinkedList<String>();
					Iterator<String> iter=userGui.keySet().iterator();
					while(iter.hasNext())
					{
						String s=iter.next();
						if(userGui.get(s).equals(msg.getSender()))
						{
							toRemove.add(s);
						}
					}
					for(int i=0;i<toRemove.size();i++)
						userGui.remove(toRemove.get(i));
					userGui.put(msgObj.UserName, msg.getSender());
					//zaakceptowanie zapisu
					ACLMessage reply=new ACLMessage(ACLMessage.AGREE);
					reply.addReceiver(msg.getSender());
					myAgent.send(reply);
					
					ACLMessage proPgt=new ACLMessage(ACLMessage.INFORM);
					try {
						proPgt.addReceiver(new AID(agentH.getName(),true));
					} catch (StaleProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try 
					{
						proPgt.setContentObject(msgObj.user.getProfile());
					} 
					catch (IOException e) 
					{
						System.out.println("Nie udalo sie utworzyc obiektu!");
					}
					myAgent.send(proPgt);
				}
			}
			else	
			{
				System.out.println("Nie ma tu zadnego obiektu");
			}
		}
		else 
		{
			block();
		}
		
	}

}
